<?php

namespace app\admin\service\auth;

use app\common\model\auth\Jobs;
use tmcore\base\BaseService;

/**
 * 岗位服务类
 */
class JobsService extends BaseService
{
    /**
     * 获取岗位列表
     * @return array
     */
    public static function getList(): array
    {
        // 搜索条件
        $searchParam = [
            '=' => ['status'],
            '%like%' => ['name']
        ];
        $where = self::getSearchData($searchParam, []);

        // 返回分页数据
        return Jobs::page($where, self::getPageData(),'*', ['sort' => 'desc', 'id' => 'desc'],['status_desc']);
    }

    /**
     * 添加岗位
     * @param array $params
     * @return bool
     */
    public static function add(array $params)
    {
        return Jobs::add([
            'name' => $params['name'],
            'coding' => $params['coding'],
            'desc' => $params['desc'],
            'status' => $params['status'],
            'sort' => $params['sort'] ?? 0
        ]);
    }

    /**
     * 编辑岗位
     * @param array $params
     * @return bool
     */
    public static function edit(array $params) : bool
    {
        return Jobs::edit([
            'name' => $params['name'],
            'coding' => $params['coding'],
            'desc' => $params['desc'],
            'status' => $params['status'],
            'sort' => $params['sort'] ?? 0
        ], $params['id']);
    }

    /**
     * 删除岗位
     * @param array $params
     * @return bool
     */
    public static function delete(array $params)
    {
        return Jobs::del($params['id']);
    }

    /**
     * 查看岗位详情
     * @param $params
     * @return array
     */
    public static function detail($params) : array
    {
        return Jobs::findOrEmpty($params['id'])->toArray();
    }

    /**
     * 岗位数据
     * @return array
     */
    public static function getAllData()
    {
        return Jobs::where(['status' => 1])
            ->field('id, name')
            ->order(['sort' => 'desc', 'id' => 'desc'])
            ->select()
            ->toArray();
    }
}